package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.checkerframework.checker.units.qual.A;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.Model;
import org.dromara.dao.business.domain.ModelParam;
import org.dromara.dao.business.domain.ModelParamQuery;
import org.dromara.dao.business.mapper.ModelMapper;
import org.dromara.dao.business.mapper.ModelParamMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class ModelParamService {

    @Autowired
    ModelParamMapper modelParamMapper;
    @Autowired
    ModelMapper modelMapper;


    public TableDataInfo<ModelParam> queryPageList(ModelParamQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                modelParamMapper.paginationSelectList(query, pageQuery),
                modelParamMapper.paginationSelectCount(query)
        );
    }


    public ModelParam queryById(Long id) {
        return modelParamMapper.selectById(id);
    }


    @Transactional
    public ModelParam insert(ModelParam entity) {
        if (entity.getModelId() != null) {
            Model model = modelMapper.selectById(entity.getModelId());
            if (model != null) {
                entity.setModelName(model.getName());
            }
        }
        modelParamMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(ModelParam update) {

        ModelParam entity = modelParamMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setTitle(update.getTitle());
        entity.setPromt(update.getPromt());
        entity.setIcon(update.getIcon());
        entity.setModelId(update.getModelId());
        if (entity.getModelId() != null) {
            Model model = modelMapper.selectById(entity.getModelId());
            if (model != null) {
                entity.setModelName(model.getName());
            }
        }
        entity.setUpdateTime(LocalDateTime.now());
        modelParamMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        modelParamMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        modelParamMapper.deleteByIds(Arrays.asList(ids));
    }


    public List<ModelParam> queryAll(Long modelId) {
        return modelParamMapper.selectList(new LambdaQueryWrapper<ModelParam>().eq(ModelParam::getModelId, modelId));
    }
}
